package org.vectrics.user.web.struts;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionForm;
import org.vectrics.user.PasswordEncryptor;
import org.vectrics.user.Person;

public class UserPasswordForm extends VectricsActionForm {

	private static final long serialVersionUID = 8791892520027166961L;
	private String personId = null;
	private String userName = null;
	private String personName = null;
	private String oldPassword = null;
	private String password = null;
	private String password2 = null;
	private Logger log = Logger.getLogger(this.getClass());
	
	
	public String getPersonId() {
		return personId;
	}
	
	public void setPersonId(String personId) {
		this.personId = personId;
	}
	
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	private Person getPerson() {
		return(CoreServiceLocator.getPersonService().findPersonById(new Long(this.getPersonId())));
	}
	
	@Override
	public ActionErrors doValidate(ActionMapping mapping, Request request) {
		ActionErrors errors = super.doValidate(mapping, request);
		Domain domain = ThreadProperties.getDomain();
		
		Person person = this.getPerson();
		
        // Si presiono cancelar va a cancel
        if (this.wasButtonPressed(request, "cancel")) {
        	return errors;
        }
		
		if ((getOldPassword() == null) || (getOldPassword().trim().length() <= 0)) {
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.old.password.required"));			
			} else {
				String oldPassword = getOldPassword();
				if (person.getUser().getPasswordEncrypted()) {
					PasswordEncryptor encryptor = new PasswordEncryptor();
					oldPassword = encryptor.crypt(oldPassword);
				}
				if (oldPassword.equals(person.getUser().getPassword()) == false) {
					errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.old.password.wrong"));			
				}
		}

		
		if ((getPassword() == null) || (getPassword().trim().length() <= 0)) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.password.required"));			
		}
		else if ((getPassword() != null) && (getPassword().length() < UserProfileForm.MINIMUM_PASSWORD_SIZE)) {
			log.debug("validate() - password too short");
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.password.tooshort"));
			//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.tooshort"));			
		}
		
		if ((getPassword2() == null) || (getPassword2().trim().length() <= 0)) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.password2.required"));			
		}
		
		if ((getPassword() != null) && (getPassword().equals(getPassword2()) == false)) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.password.match"));
			
		}
		// Si hay errores lleno el formulario con los datos por defecto.
		if (!errors.isEmpty()) {
			this.setPersonId(person.getId().toString());
			this.setUserName(person.getUser().getUserName());
			this.setPersonName(person.getName());
		}
		
		return(errors);
	}
	
}
